package com.boarsoft.boar.soagov.svc.biz;

import java.util.List;

import org.hibernate.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.entity.AppInfo;
import com.boarsoft.boar.soagov.entity.AppSoagovInfo;
import com.boarsoft.boar.soagov.entity.AppSvc;
import com.boarsoft.boar.soagov.entity.SvcInfo;
import com.boarsoft.boar.soagov.svc.SvcAppBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("svcAppBiz")
public class SvcAppBizImpl extends SimpleBizImpl implements SvcAppBiz {

	@Override
	@Transactional(readOnly = true)
	public PagedResult<AppInfo> list(String svcId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(AppInfo.class.getName());
		sb.append(" a join a.svcs s where s.id='").append(svcId).append("'");
		if (Util.strIsNotEmpty(key)) {
			sb.append("and (a.code like '%").append(key);
			sb.append("%' or a.name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal("select count(a.id)".concat(sb.toString()));
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by a.").append(orderBy);
		} else {
			sb.append(" order by a.name");
		}
		List<AppInfo> lt = dao.list("select a".concat(sb.toString()), pageNo, pageSize);
		return new PagedResult<AppInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public List<AppInfo> list(String svcId, short status) {
		StringBuilder sb = new StringBuilder();
		sb.append("select a from ").append(AppInfo.class.getName());
		sb.append(" a join a.svcs s where s.id='").append(svcId).append("'");
		sb.append(" and a.status=").append(status);
		return dao.list(sb.toString());
	}

	@Override
	@Transactional(readOnly = true)
	public List<AppInfo> list(String svcId) {
		StringBuilder sb = new StringBuilder();
		sb.append("select a from ").append(AppInfo.class.getName());
		sb.append(" a join a.svcs s where s.id='").append(svcId).append("'");
		return dao.list(sb.toString());
	}

	@Override
	@Transactional
	public void add(String appId, String[] svcIds) {
		StringBuilder sb = new StringBuilder().append("from ")//
				.append(AppSvc.class.getName()).append(" where appId=:appId and svcId=:svcId");
		Query<AppSvc> q = dao.createQuery(AppSvc.class, sb.toString());
		q.setParameter("appId", appId);
		for (String svcId : svcIds) {
			q.setParameter("svcId", svcId);
			AppSvc as = q.uniqueResult();
			if (as == null) {
				as = new AppSvc();
				as.setSvcId(svcId);
				as.setAppId(appId);
				dao.save(as);
			}
		}
	}

	@Override
	@Transactional(readOnly = true)
	public AppSvc get(String id) {
		return (AppSvc) dao.get(AppSvc.class, id);
	}

	@Override
	@Transactional
	public void remove(String id, String[] objIds) {
		AppSoagovInfo a = dao.get(AppSoagovInfo.class, id);
		for (String k : objIds) {
			SvcInfo c = (SvcInfo) dao.get(SvcInfo.class, k);
			if (a.getSvcs().contains(c)) {
				a.getSvcs().remove(c);
			}
		}
	}

	@Override
	@Transactional
	public void delete(String svcId) {
		StringBuffer sb = new StringBuffer();
		sb.append("delete from  ").append(AppSvc.class.getName()).append(" acm where acm.svc.id='").append(svcId).append("'");
		dao.executeUpdate(sb.toString());
	}
}